#include "driver_timer.h"
#include "stm32f10x.h"


void timer_init()
{
    // 使能TIM2
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
	// 配置时钟源
	TIM_InternalClockConfig(TIM2);
	
	//配置1ms中断一次
    TIM_TimeBaseInitTypeDef stTimeInitDef;
	stTimeInitDef.TIM_Prescaler = 720 - 1;	// 100 000
	stTimeInitDef.TIM_Period = 1000 - 1;
	stTimeInitDef.TIM_ClockDivision = TIM_CKD_DIV1;
	stTimeInitDef.TIM_CounterMode = TIM_CounterMode_Up;
	stTimeInitDef.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM2, &stTimeInitDef);
	
	//配置中断
	TIM_ClearFlag(TIM2, TIM_IT_Update);
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	
	NVIC_InitTypeDef stNVICInitDef;
    stNVICInitDef.NVIC_IRQChannel = TIM2_IRQn;
	stNVICInitDef.NVIC_IRQChannelCmd = ENABLE;
	stNVICInitDef.NVIC_IRQChannelPreemptionPriority = 2;
	stNVICInitDef.NVIC_IRQChannelSubPriority = 0;
	NVIC_Init(&stNVICInitDef);

	TIM_Cmd(TIM2, ENABLE);
}





